package es.efor.ics.gev.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Version;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * The persistent class for the <b>CATEGORIAS_ATRIBUTO</b> database table.
 * 
 * @author dlisbona
 */
@Entity()
@Table(name = "CATEGORIAS_ATRIBUTO")
public class CategoriaAtributo extends BaseObject implements Serializable {
	
	// static data
	private static final long serialVersionUID = 7473921342035610887L;

	// private instance data
	private Long id;
	private String nombreCategoria;		// required	
	private Set<AtributoMaestro> atributos = new HashSet<AtributoMaestro>();
	private Integer version;
	
	/**
	 * Construct an uninitialized <code>CategoriaAtributo</code> (for runtime proxy
	 * generation in Hibernate). 
	 */
	protected CategoriaAtributo() {	
	}
	
	/**
	 * Construct an initialized <code>CategoriaAtributo</code>.
	 * 
	 * @param nombreCategoria
	 */
	public CategoriaAtributo(String nombreCategoria) {
		this.nombreCategoria = nombreCategoria;
	}

	// ********************** Accessor Methods ********************** //
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "CATEGORIA_ATRIBUTO_ID")
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}	
	
	// Unique, not nullable
	@Column(name = "NOMBRE_CATEGORIA", length = 255, unique = true, nullable = false)
	public String getNombreCategoria() {
		return nombreCategoria;
	}

	public void setNombreCategoria(String nombreCategoria) {
		this.nombreCategoria = nombreCategoria;
	}

	/**
	 * Read-only
	 * 
	 * @see AtributoMaestro#addCategoria(CategoriaAtributo)
	 * @return set de atributos
	 */
	@ManyToMany(mappedBy = "categorias", fetch = FetchType.LAZY)	
	@org.hibernate.annotations.LazyCollection(value = org.hibernate.annotations.LazyCollectionOption.TRUE)
	public Set<AtributoMaestro> getAtributos() {
		return atributos;
	}

	public void setAtributos(Set<AtributoMaestro> atributos) {
		this.atributos = atributos;
	}

	/**
	 * Borra todos los {@link AtributoMaestro atributos} asociados a una categoria 
	 * 
	 * @param update iff <code>true</code> actualiza la relacion inversa
	 */
	public void removeAtributos(boolean update) {
		for (AtributoMaestro atributo:atributos) {
			if (update) {
				atributo.getCategorias().remove(this);
			}
		}
		atributos.clear();
	}
	
	@Version
	@Column(name = "OBJ_VERSION")
	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}
	
	// ********************** Common Methods ********************** //
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof CategoriaAtributo == false) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		CategoriaAtributo categoria = (CategoriaAtributo) obj;
		return new EqualsBuilder().append(this.getNombreCategoria(),
				categoria.getNombreCategoria()).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(17, 37)
		.append(this.getNombreCategoria()).toHashCode();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).append(
				"nombreCategoria", this.getNombreCategoria()).toString();
	}

}
